package com.gccloud.design;

import com.alibaba.fastjson.JSON;
import com.gccloud.design.entity.InterfaceEntity;
import com.gccloud.design.service.InterfaceQueryService;
import com.gccloud.design.vo.InterfaceVo;
import com.gccloud.starter.common.dto.SearchDTO;
import com.gccloud.starter.common.utils.BeanConvertUtils;
import com.gccloud.starter.common.vo.R;
import com.gccloud.starter.core.vo.PageVO;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 接口配置API
 *
 * @author zhu.liangyu
 */
@Slf4j
@RestController
@RequestMapping("/intf")
@Api(tags = "接口配置API")
@ApiSort(value = 10)
public class InterfaceController {

    @Resource
    private InterfaceQueryService interfaceQueryService;

    @ApiOperation(value = "分页查询接口配置", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "页码", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = "searchKey", value = "查询条件", paramType = "query", dataType = "string")
    })
    public R<PageVO<InterfaceVo>> queryIntfByPageList(SearchDTO searchDTO) {
        PageVO<InterfaceEntity> pageList = interfaceQueryService.getInterfaceList(searchDTO);
        PageVO pageVo = BeanConvertUtils.convert(pageList, PageVO.class);
        pageVo.setList(pageList.getList());
        return R.success(pageVo);
    }

    @ApiOperation(value = "新增接口配置")
    @PostMapping("/add")
    public R<Object> add(@RequestBody InterfaceVo vo) {

        if (interfaceQueryService.getInterfaceByCode(vo.getIoCd()) != null) {
            return R.error(500, "接口编码已存在");
        }
        InterfaceEntity entity = BeanConvertUtils.convert(vo, InterfaceEntity.class);
        boolean result = interfaceQueryService.saveInterfaceInfo(entity);
        if (result) {
            return R.success(entity.getIoId());
        } else {
            return R.error(500, "新增接口配置失败");
        }
    }

    @ApiOperation(value = "查询接口配置", notes = "根据接口编码code查询接口配置")
    @GetMapping("/get/{code}")
    @ApiImplicitParam(name = "code", value = "接口编码code", required = true, dataType = "String", paramType = "path")
    public R<InterfaceVo> getByCd(@PathVariable("code") String code) {
        InterfaceEntity entity = interfaceQueryService.getInterfaceByCode(code);
        if (entity == null) {
            return R.success();
        }
        InterfaceVo interfaceVo = BeanConvertUtils.convert(entity, InterfaceVo.class);
        return R.success(interfaceVo);
    }

    @ApiOperation(value = "修改接口配置")
    @PutMapping("/update")
    public R<Object> update(@RequestBody InterfaceVo vo) {
        InterfaceEntity entity = BeanConvertUtils.convert(vo, InterfaceEntity.class);
        boolean result = interfaceQueryService.updateInterfaceInfo(entity);
        if (result) {
            return R.success();
        } else {
            return R.error(500, "修改接口配置失败");
        }
    }

    @ApiOperation(value = "删除接口配置", notes = "根据接口Id删除接口配置")
    @DeleteMapping("/delete/{ioId}")
    @ApiImplicitParam(name = "ioId", value = "接口Id", required = true, dataType = "Long", paramType = "path")
    public R<Object> delete(@PathVariable("ioId") Long ioId) {
        return R.success(interfaceQueryService.delInterface(ioId));
    }

    /**
     * 保存测试参数
     */
    @PostMapping(value = "/saveInterfaceParam")
    @ApiOperation(value = "保存接口入参", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ioId", value = "接口id", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = "comments", value = "测试入参", paramType = "query", required = true, dataType = "string")
    })
    public R<Object> saveInterfaceParam(@RequestParam(value = "comments") String comments,
                                        @RequestParam(value = "ioId") String ioId) {
        interfaceQueryService.saveInterfaceParam(comments, ioId);
        return R.success();
    }


    /**
     * 接口测试(待完善)
     */
    @PostMapping(value = "/testInterface")
    @ApiOperation(value = "测试接口", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ioData", value = "接口信息", paramType = "query", required = true, dataType = "string"),
            @ApiImplicitParam(name = "dataParams", value = "测试入参", paramType = "query", required = true, dataType = "string")
    })
    public R<Object> testInterface(@RequestParam(value = "dataParams") String dataParams,
                                   @RequestParam(value = "ioData") String ioData) {
        log.info("接口信息为：{},dataParams为：{}", ioData, dataParams);
        //转换成map
        Map map = JSON.parseObject(ioData, Map.class);
        log.info("接口类型为：{}", map.get("scriptType"));
        return R.success();
    }
}
